package com.jzo2o.market.controller.activity;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.market.model.domain.Activity;
import com.jzo2o.market.model.domain.Coupon;
import com.jzo2o.market.model.domain.CouponWriteOff;
import com.jzo2o.market.model.dto.request.ActivityQueryForPageReqDTO;
import com.jzo2o.market.model.dto.request.ActivitySaveReqDTO;
import com.jzo2o.market.model.dto.response.ActivityInfoResDTO;
import com.jzo2o.market.service.IActivityService;
import com.jzo2o.market.service.ICouponService;
import com.jzo2o.market.service.ICouponWriteOffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 优惠券活动相关接口
 *
 * @author itcast
 * @create 2023/7/7 19:34
 **/
@RestController("marketActivityController")
@RequestMapping("/operation/activity")
@Api(tags = "优惠券活动相关接口")
@Slf4j
public class MarketActivityController {

    @Autowired
    private IActivityService activityService;

    @Autowired
    private ICouponService couponService;

    @Autowired
    private ICouponWriteOffService couponWriteOffService;

    @ApiOperation("保存优惠券活动接口")
    @PostMapping("/save")
    public void place(@RequestBody ActivitySaveReqDTO activitySaveReqDTO) {
        Activity activity = BeanUtil.toBean(activitySaveReqDTO, Activity.class);
        activity.setStockNum(activitySaveReqDTO.getTotalNum());
        activity.setStatus(1);
        activityService.save(activity);
    }

    /**
     * 优惠券活动分页查询接口
     * @param
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("优惠券活动分页查询接口")
    public PageResult<Activity> selectPage(ActivityQueryForPageReqDTO activityQueryForPageReqDTO) {
        return activityService.pageQury(activityQueryForPageReqDTO);
    }

    /**
     * 优惠券活动详情接口
     * @param
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("优惠券活动详情接口")
    public ActivityInfoResDTO selectOne(@PathVariable("id") Long id) {
        Activity activity = activityService.getById(id);
        int count = couponWriteOffService.count(Wrappers.<CouponWriteOff>lambdaQuery()
                .eq(CouponWriteOff::getActivityId, id));
        int count1 = couponService.count(Wrappers.<Coupon>lambdaQuery()
                .eq(Coupon::getActivityId, id));
        ActivityInfoResDTO activityInfoResDTO = BeanUtil.toBean(activity, ActivityInfoResDTO.class);
        activityInfoResDTO.setReceiveNum(count1);
        activityInfoResDTO.setWriteOffNum(count);
        return activityInfoResDTO;
    }

    /**
     * 撤销活动接口
     *
     * @param
     * @return
     */
    @ApiOperation("撤销活动接口")
    @PostMapping("/revoke/{id}")
    public void place(@PathVariable("id") Long id) {
        // 如果活动状态为带生效或者进行中就可以进行撤销，状态变为作废
        Activity activity = activityService.getById(id);
        if (ObjectUtil.isEmpty(activity)){
            throw new RuntimeException("活动不存在");
        }
        // 活动状态为待生效或者进行中
        if (activity.getStatus().equals(1) || activity.getStatus().equals(2)) {
            boolean update = activityService.update(Wrappers.<Activity>lambdaUpdate()
                    .eq(Activity::getId, id)
                    .set(Activity::getStatus, 4));
            if (update) {
                log.info("活动:{} 作废！！", id);
                // 该活动所有未使用的优惠券变成已作废
                boolean update1 = couponService.update(Wrappers.<Coupon>lambdaUpdate()
                        .eq(Coupon::getActivityId, id)
                        .eq(Coupon::getStatus, 1)
                        .set(Coupon::getStatus, 3));
                if (update1) {
                    log.info("活动:{} 的未使用优惠券作废成功！", id);
                }
            }
        }
    }
}
